package weibo.service;
import weibo.database.UserDB;
import weibo.model.UserMod;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class reguser
 */
@WebServlet("/reguser")
public class reguser extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public reguser() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.getRequestDispatcher("./reg.html").forward(request, response);
	}

	/**
	 * 用户注册
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doGet(request, response);
		UserMod user=new UserMod();
		UserDB db=new UserDB();
		String login_name=request.getParameter("login_name");
		String user_name=request.getParameter("user_name");
		String password=request.getParameter("password");
		String borndate=request.getParameter("borndate");
		String sex=request.getParameter("sex");
		String simple=request.getParameter("simple");
		String ucode=request.getParameter("code").toUpperCase();
		
		HttpSession session=request.getSession(true);
		Object code=session.getAttribute("code");
		if (!ucode.equals(code)){
			System.out.println(ucode+code);
			response.getWriter().append("codeerror");
			return;
		}
		
		user.setLogin_name(login_name);
		user.setUser_name(user_name);
		user.setPassword(password);
		user.setBorndate(borndate);
		user.setSex(sex);
		user.setSimple(simple);
		boolean bool=db.CreateUser(user);
		if (bool){
			response.getWriter().append("success");
			System.out.println("用户注册成功");
		}else{
			response.getWriter().append("error");
			System.out.println("用户注册失败");
		}
	}

}
